package com.idearworld.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.idearworld.beans.SplitPage;
import com.idearworld.dao.FollowIdearMapMapper;
import com.idearworld.dao.IdearsMapper;
import com.idearworld.model.FollowIdearMap;
import com.idearworld.model.Idears;
import com.idearworld.model.IdearsExample;
import com.idearworld.model.UserInfo;
import com.idearworld.service.IdearService;


@Service("idearService")
public class IdearServiceImpl implements IdearService{
	
	private IdearsMapper idearsMapper;
	private FollowIdearMapMapper followIdearMapMapper;
	
	@Autowired
    public void setFollowIdearMapMapper(FollowIdearMapMapper followIdearMapMapper) {
		this.followIdearMapMapper = followIdearMapMapper;
	}



	@Autowired	
	public void setIdearsMapper(IdearsMapper idearsMapper) {
		this.idearsMapper = idearsMapper;
	}



	@Override
	public int createIdear(Idears idear) {
		
		return idearsMapper.insert(idear);
	}



	@Override
	public List<Idears> browseIdear(SplitPage sp) {
	    IdearsExample example=new IdearsExample();
	    
	    example.or().andIdearIdGreaterThan(1);
	    example.setOrderByClause("create_date desc"+sp.getSplitString());
		return this.idearsMapper.selectByExample(example);
	}



	@Override
	public Idears getIdearById(Integer id) {
		return this.idearsMapper.selectByPrimaryKey(id);
	}



	@Override
	public int followIdear(int idearId, int followerId) {
		FollowIdearMap followIdearMap=new FollowIdearMap();
		followIdearMap.setIdearId(idearId);
		followIdearMap.setUserId(followerId);
		followIdearMap.setFollowDate(new Date());
		
		return this.followIdearMapMapper.insert(followIdearMap);
	}



	@Override
	public List<Idears> listUserIdears(UserInfo user,SplitPage sp) {
		IdearsExample example=new IdearsExample();
		example.or().andUserIdEqualTo(user.getUserId());
		example.setOrderByClause("create_date desc"+sp.getSplitString());
		return this.idearsMapper.selectByExample(example);
	}

	
}
